//
//  PushForHYNR.m
//  HYHoney
//
//  Created by dllo on 16/2/27.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "PushForHYNR.h"
#import "ModelForPush.h"
#import "UIImageView+WebCache.h"
#import "NetworkHandeler.h"
#import "CellForNROfImg.h"
#import "MJRefresh.h"
#import "PushForPush.h"
#import "DKNightVersionManager.h"
#import "DKNightVersion.h"

@interface PushForHYNR ()<UITableViewDelegate, UITableViewDataSource, NetworkHandelerDelegate>

@property (nonatomic, retain)NSMutableArray *arr;
@property (nonatomic, retain)UIImageView *imageZX;
@property (nonatomic, retain)UITableView *tableView;
@property (nonatomic, assign)BOOL isRefreshing;   // 刷新状态

@end

@implementation PushForHYNR

- (void)dealloc{

    [_arr release];
    [_imageZX release];
    [_tableView release];
    [super dealloc];
}



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    [self createTableView];
    [self handleData];
}

- (void)createTableView{
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64 - 50) style:UITableViewStylePlain];
    
    [self.view addSubview:self.tableView];
    [self.tableView release];
    
    [self.tableView registerClass:[CellForNROfImg class] forCellReuseIdentifier:@"pool"];
    
    
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    self.tableView.showsVerticalScrollIndicator = NO;
    
    self.tableView.dk_backgroundColorPicker =  DKColorWithRGB(0xFFFAFA, 0x27408B);
//    self.tableView.dk_separatorColorPicker = DKColorWithRGB(0xaaaaaa, 0x313131);
    
    // 实现刷新功能
    self.isRefreshing = NO;
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        // 如果进入刷新状态, 即自动调用此块代码.
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            [self.tableView.mj_header endRefreshing];
            
            self.isRefreshing = YES;
        });
    }];
    
    
    [self.tableView.mj_header beginRefreshing];
    
}


- (void)handleData{

    NSString *str = [NSString stringWithFormat:@"http://api.bohejiaju.com/v1/collections/%ld/posts?gender=2&generation=4&limit=20&offset=0",self.idN];
    
    [NetworkHandeler handelerJSONWithURL:str delegate:self];
}


- (void)handelerDidComplete:(id)result{

    self.arr = [NSMutableArray array];
    
    NSDictionary *dic = [result objectForKey:@"data"];
    NSArray *arr = [dic objectForKey:@"posts"];
    
    for (NSDictionary *dicH in arr) {
        
        ModelForPush *model = [[ModelForPush alloc] init];
        
        model.content_url = [dicH objectForKey:@"content_url"];
        model.cover_image_url = [dicH objectForKey:@"cover_image_url"];
        model.title = [dicH objectForKey:@"title"];
        model.likes_count = [dicH objectForKey:@"likes_count"];
        
        [self.arr addObject:model];
    }
    [self.tableView reloadData];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    return self.arr.count;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    CellForNROfImg *cell = [tableView dequeueReusableCellWithIdentifier:@"pool"];
    
    ModelForPush *model = [self.arr objectAtIndex:indexPath.row];
    
    [cell passModelPush:model];
    
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    PushForPush *push2 = [[PushForPush alloc] init];
    
    ModelForPush *model = [self.arr objectAtIndex:indexPath.row];
    [push2 passModelForPush:model];
    
    [self.navigationController pushViewController:push2 animated:NO];
    
    [push2 release];
}



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 260;
    
}


- (void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
}


- (void)viewWillDisappear:(BOOL)animated{
    
    [super viewWillDisappear:animated];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
